Python ImportError: cannot import name urlopen错误分析 您所在的位置:网站首页 import request报错 Python ImportError: cannot import name urlopen错误分析

Python ImportError: cannot import name urlopen错误分析

2022-11-06 03:54| 来源: 网络整理| 查看: 265

在学习python的socket编程过程中遇到一个错误,错误的提示信息如下:

ImportError: cannot import name urlopen,出现该问题的原因,主要是因为我的目录下有一个socket.py的问题,这个文件正好和socke模块中的socket.py文件名重名导致的。

root@team:~/python# ls

char_format.py  numberlines.py  socket.pyc    test_help.py   test_search.py   test_translate.py

drawing         re_split.py     test_copy.py  test_if.py     test_sequeue.py  test_wordbook1.py

heap.py         reverseargs.py  test_fibs.py  test_index.py  test_slices.py   test_wordbook.py

hello.py        socket.py       test_for.py   test_in.py     test_sys.py

root@team:~/python# socket.py

Traceback (most recent call last):

  File "./socket.py", line 2, in

    from urllib import urlopen

  File "/usr/lib/python2.6/urllib.py", line 26, in

    import socket

  File "/home/zfz/python/socket.py", line 2, in

    from urllib import urlopen

ImportError: cannot import name urlopen

出现上面的问题,可以查阅一下python查找模块的方式,找到下面一段话:

Actually, modules are searched in the list of directories given by the variable sys.path which is initialized from the directory containing the input script (or the current directory), PYTHONPATH and the installation- dependent default. This allows Python programs that know what they’re doing to modify or replace the module search path. Note that because the directory containing the script being run is on the search path, it is important that the script not have the same name as a standard module, or Python will attempt to load the script as a module when that module is imported.

首先,在使用import导入模块时,Python使用PYTHONPATH的搜索路径,我们可以通过下面的命令,查看Python的搜索路径:

import sys,pprint

>>> pprint.pprint(sys.path)

['', //表示的是当前路径,也就是现在python测试程序的路径

 '/usr/lib/python2.6',

 '/usr/lib/python2.6/plat-linux2',

 '/usr/lib/python2.6/lib-tk',

 '/usr/lib/python2.6/lib-old',

 '/usr/lib/python2.6/lib-dynload',

 '/usr/lib/python2.6/dist-packages',

 '/usr/lib/pymodules/python2.6',

 '/usr/lib/pymodules/python2.6/gtk-2.0',

 '/usr/local/lib/python2.6/dist-packages']

上面的问题就是因为,在Import模块式,先搜索的是home/zfz/python目录,在该目录下也正好有个socket.py的Python文件。

对于模块的导入会有下面的情况,在使用自己编写的python模块时,在另个文件中进行导入操作,对应的导入文件会生成.pyc结尾的文件。假设我们有个python的测试文件test_import_exam.py文件,需要导入test_import.py,

python# cat test_import_exam.py

#!/usr/bin/python

import test_import

root@team:~/python# cat test_import.py

#!/usr/bin/python

 

root@team:~/python# cat test_import.pyc

?

+WSc@s

dS

第一次导入test_import时,test_import.py就会被编译为字节码,此时该目录下就会出现一个名为test_import.py的文件,因为*.pyc格式比*.py格式导出的速度更快,如果更新了test_import.py则必须把它之前被编译的字节码test_import.pyc删除,否则在使用这个模块的时候就不会看到更新的部分,因为它是从第一次生成的test_import.pyc里面导出来;必须对新的test_import.py脚本编译成*.pyc文件或是直接把test_import.py(第一次生成)删掉,python解释器会自动没有没有相应*.pyc的*.py文件生成相应的*.pyc文件(基本规则:当导入一个模块时,python解释器先把*.py文件编译成*.pyc,然后在从*.pyc里面导出)。编译出来的pyc文件都是字节码文件,当使用cat打印该文件时,查看到的是乱码。

什么是pyc文件

pyc是一种二进制文件,是由py文件经过编译后,生成的文件,是一种byte code,py文件变成pyc文件后,加载的速度有所提高,而且pyc是一种跨平台的字节码,是由python的虚拟机来执行的,这个是类似于JAVA或者.NET的虚拟机的概念。pyc的内容,是跟python的版本相关的,不同版本编译后的pyc文件是不同的,2.5编译的pyc文件,2.4版本的python是无法执行的。

                                                                                                                                        



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有